package cn.ctvonline.android.kaopu.support;

import android.content.Context;

import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;
import com.amap.api.location.AMapLocationListener;

/**
 * Created by dujian on 15/11/26.
 */
public class AmapLocationManager {
    private static AmapLocationManager instance = new AmapLocationManager();
    //声明AMapLocationClient类对象
    public AMapLocationClient mLocationClient = null;
    //声明定位回调监听器
    public AMapLocationListener mLocationListener;
    //声明mLocationOption对象
    public AMapLocationClientOption mLocationOption = null;

    private AmapLocationManager() {

    }

    public static AmapLocationManager getInstance() {
        return instance;
    }

    public void init(Context context) {
        if (mLocationClient == null) {
            //初始化定位
            mLocationClient = new AMapLocationClient(context);

        }
        mLocationOption = new AMapLocationClientOption();
        //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);
        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);
        //设置是否只定位一次,默认为false
        mLocationOption.setOnceLocation(false);
        //设置是否强制刷新WIFI，默认为强制刷新
        mLocationOption.setWifiActiveScan(true);
        //设置是否允许模拟位置,默认为false，不允许模拟位置
        mLocationOption.setMockEnable(false);
        //设置定位间隔,单位毫秒,默认为2000ms
        mLocationOption.setInterval(2000);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
    }

    public void startLocation(AMapLocationListener aMapLocationListener) {
        if (aMapLocationListener != null) {
            mLocationClient.setLocationListener(aMapLocationListener);
        }
        //启动定位
        mLocationClient.startLocation();
    }

    /**
     * 停止定位
     */
    public void stopLocation() {
        if (mLocationClient != null) {
            mLocationClient.stopLocation();//停止定位
        }
    }

    public void destoryLocation() {
        stopLocation();
        if (mLocationClient != null) {
            mLocationClient.onDestroy();
        }
        mLocationClient = null;
        instance = null;
    }

    public AMapLocationListener getmLocationListener() {
        return mLocationListener;
    }

    public void setmLocationListener(AMapLocationListener mLocationListener) {
        this.mLocationListener = mLocationListener;
        if (mLocationClient != null) {
            mLocationClient.setLocationListener(mLocationListener);
        }
    }

}